package org.elzat.healthboxgatewayapp.dto;

import lombok.Builder;
import lombok.Data;
import org.elzat.healthboxgatewayapp.credentials.HealthBoxUserRole;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;

@Builder
public class HealthBoxUser implements UserDetails {

    // credential information
    private Long id;
    private String password;
    private String userPhone;
    private Boolean locked;
    private HealthBoxUserRole role;

    // other information
    private String name;
    private String idNumber;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return role.getAuthorities();
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return userPhone;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public Long getId() {
        return id;
    }

    public static HealthBoxUser fromEntity(HealthBoxUserEntity entity) {
        return HealthBoxUser.builder()
                .id(entity.getId())
                .userPhone(entity.getUserPhone())
                .password(entity.getPassword())
                .role(HealthBoxUserRole.valueOf(entity.getRole()))
                .name(entity.getName())
                .idNumber(entity.getIdNumber())
                .locked(entity.getLocked())
                .build();
    }

    public HealthBoxUserVO toVO() {
        return HealthBoxUserVO.builder()
                .id(id)
                .userPhone(userPhone)
                .locked(locked)
                .name(name)
                .idNumber(idNumber)
                .role(role)
                .build();
    }
}
